/*----------------------------------------------------------------------------*\
| /gamemode/cl_postprocess.lua         | post-processing effects         | CLI |
\*----------------------------------------------------------------------------*/

sh1 = 0.75
sh2 = 0.75

zday.PP = {}

zday.PP.Normal	= {    0,    0,    0,    0, 1, 1,    0,    0,    0 }
//zday.PP.Zombie	= { 0.12, 0.16, 0.08, -0.3, 2, 1, 0.16, 0.33, 0.22 } // v1
zday.PP.Zombie	= { 0.39, 0.38, 0.27, -0.1, 0.24, 2.31, 0.84, 0.59, 0 } // v2
//zday.PP.Zombie	= { 38, 35, 21, -1.03, 0.7, 82, 0, 0, 0 }
zday.PP.Actual	= table.Copy(zday.PP.Normal)
zday.PP.Last	= table.Copy(zday.PP.Actual)

local matBlurEdges		= Material( "bluredges" )
local tex_MotionBlur	= render.GetMoBlurTex0()

MotionBlur = 0
MBlurDelta = 0

ColorModify = {}
ColorModify[ "$pp_colour_addr" ]            = 0
ColorModify[ "$pp_colour_addg" ]            = 0
ColorModify[ "$pp_colour_addb" ]            = 0
ColorModify[ "$pp_colour_brightness" ]      = 0
ColorModify[ "$pp_colour_contrast" ]        = 1
ColorModify[ "$pp_colour_colour" ]          = 1
ColorModify[ "$pp_colour_mulr" ]            = 0
ColorModify[ "$pp_colour_mulg" ]            = 1
ColorModify[ "$pp_colour_mulb" ]            = 1

OriginalColorModify = ColorModify

function zday.PP.ColourMod( colmod )
	ColorModify[ "$pp_colour_addr" ]        = colmod[1]
	ColorModify[ "$pp_colour_addg" ]        = colmod[2]
	ColorModify[ "$pp_colour_addb" ]        = colmod[3]
	ColorModify[ "$pp_colour_brightness" ]  = colmod[4]
	ColorModify[ "$pp_colour_contrast" ]    = colmod[5]
	ColorModify[ "$pp_colour_colour" ]      = colmod[6]
	ColorModify[ "$pp_colour_mulr" ]        = colmod[7]
	ColorModify[ "$pp_colour_mulg" ]        = colmod[8]
	ColorModify[ "$pp_colour_mulb" ]        = colmod[9]
end

local function DrawInternal()

	if (MotionBlur > 0) then
		if (MotionBlur>0) then DrawMotionBlur( 1 - MotionBlur, 1.0, 0.0 ) end
		MotionBlur=math.Approach(MotionBlur,0,MBlurDelta)
		if (MotionBlur<0.00001) then
			MotionBlur=0
		else
			MBlurDelta = MBlurDelta*1.5
		end
	end
	
	DrawColorModify( ColorModify )
	
	//DrawBloom( 0.1, 0.3, 4, 4, 1, 1, 1, 1, 1 )
	//DrawBloom(0.65,2.0,9.0,9.0,1.0,1.0,1.0,1.0)
	
	/*if (LocalPlayer():IsInfected() or INFECTING) then
		DrawSharpen(sh1,sh2)
	end*/
	
	if (YOUAREDEAD) then render.SetMaterial(matBlurEdges) end
	render.UpdateScreenEffectTexture()
	render.DrawScreenQuad()
	//return true
end
hook.Add( "RenderScreenspaceEffects", "ZDAYPP", DrawInternal )

function zday.PP.StepZombie(st)
	for i=1,9 do
		zday.PP.Actual[i]=math.Approach(zday.PP.Actual[i],zday.PP.Zombie[i],math.abs(zday.PP.Normal[i]-zday.PP.Zombie[i])/st)
	end
end

function zday.PP.StepHeal(st)
	for i=1,9 do
		zday.PP.Actual[i]=math.Approach(zday.PP.Actual[i],zday.PP.Normal[i],math.abs(zday.PP.Normal[i]-zday.PP.Zombie[i])/st)
	end
end


-- Alright who put ()'s  after the function variable in the usermessage hooks. :P BLaNK.
-- By the way this system seems full of suck and should be fixed up.

function zday.PP.StartInfecting()
	if (!INFECTING) then
		INFECTING=true
		//zday.PP.Actual=table.Copy(zday.PP.Normal)
		timer.Destroy("HealingTimer")
		timer.Create("InfectingTimer",0.1,200,zday.PP.StepZombie,200)
	end
end
usermessage.Hook("zday_startinfecting", zday.PP.StartInfecting )

function zday.PP.StopInfecting()
	if (INFECTING) then
		INFECTING=false
		//zday.PP.Actual=table.Copy(zday.PP.Zombie)
		timer.Destroy("InfectingTimer")
		timer.Create("HealingTimer",0.1,50,zday.PP.StepHeal,50)
	end
end
usermessage.Hook("zday_stopinfecting", zday.PP.StopInfecting)

-- Moved at least some of the post processing stuff to the client, saves a fair wack of network traffic and server CPU which is stacking up against us. -BLaNK.
function GM:PlayerHurt( victim, attacker, healthrem, damage )
	-- Some trixy and unlegible math in here. :D
	
	-- We dun wanna do this for other players as well :o
	if victim != LocalPlayer() then return false end
	
	damage = math.Round( damage ) -- This was happening several times so I local'ed it.
	etc.Hurting = math.Clamp( damage * 4, 0, 255 )
	MotionBlur = math.Clamp( MotionBlur + math.Clamp( damage / 20, 0, 255 ), 0, 0.75 )
	MBlurDelta = 0.00001
	if damage <= 7 then
		amount = 7 --  Not sure what is happening here, all I know is it's not going to change the health because it's running on the client now.
	end
	TARGETFOV = MYFOV + math.Clamp( damage, 0, 120 - MYFOV )
end 

function GM:Initialize()
	MYFOV = 120
	MotionBlur = 1.0
	MBlurDelta = 0.002
end
